import axios from "axios";
// import router from "../router";
import Element from "element-ui"

// axios.defaults.baseURL = "http://localhost:8082"


const request = axios.create({
    timeout: 5000,
    // headers: {
    //     'Content-Type': "application/json; charset=utf-8",
    // }
})

request.interceptors.request.use(
    (config) => {
      config.headers["API-User-Token"] = sessionStorage.getItem("token");
      config.headers["Content-Type"] = "application/json; charset=utf-8";
      return config;
    },
    (error) => {
      return Promise.reject(error);
    }
  );

// 后置拦截
request.interceptors.response.use(response => {
        console.log("response ->" + response)
        // 这里是response的拦截
        let res = response
        console.log(res);
        if (res.status == 200) {
            return res.data
        } else {
            Element.Message.error(!res.msg ? '系统异常' : res.msg)
            // 拒绝流程继续往下走
            return Promise.reject(response.data.msg)
        }
    },
    // 如果业务中出现异常
    error => {
        // 先看后端返没返报错信息，返了就用后端的
        if (error.response.data) {
            error.message = error.response.data.msg
        }

        // 401没权限
        // if (error.response.status === 401) {
        //     router.push("/login")
        // }

        Element.Message.error(error.message, {duration:3000})

        return Promise.reject(error)

    }
)


export default request


